#include "../../Base/stdafx.h"
#include "Triangle.h"
#include "../../Base/GameEngine.h"

Triangle::Triangle(const D3DXVECTOR3& pos, float scale): m_Pos(pos), m_Scale(scale), 
	m_EffectPtr(0), m_TechniekPtr(0), m_VertexBufferPtr(0), m_VertexLayoutPtr(0)
{
	Init();
}

Triangle::~Triangle()
{
	if(m_EffectPtr) m_EffectPtr->Release();
	if(m_VertexBufferPtr) m_VertexBufferPtr->Release();
	if(m_VertexLayoutPtr) m_VertexLayoutPtr->Release();
}

void Triangle::Draw()
{
	DXDevice::GetSingleton()->GetDevice()->IASetInputLayout(m_VertexLayoutPtr);
	UINT stride = sizeof(SimpleVertex);
	UINT offset = 0;
	DXDevice::GetSingleton()->GetDevice()->IASetVertexBuffers(0, 1, &m_VertexBufferPtr, &stride, &offset);
	DXDevice::GetSingleton()->GetDevice()->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	D3D10_TECHNIQUE_DESC tech;
	m_TechniekPtr->GetDesc( &tech);
	for(UINT t = 0; t < tech.Passes; ++t)
	{
		m_TechniekPtr->GetPassByIndex(t)->Apply(0);
		DXDevice::GetSingleton()->GetDevice()->Draw(3, 0);
	}
}

void Triangle::Init()
{
	// Create effect
	DWORD ShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined(_DEBUG) || defined(DEBUG)
	ShaderFlags |= D3D10_SHADER_DEBUG;
#endif
	HRESULT hr = D3DX10CreateEffectFromFile(_T("./Shaders/pos.fx"), NULL, 
		NULL, "fx_4_0", ShaderFlags, NULL, DXDevice::GetSingleton()->GetDevice(), 
		NULL, NULL, &m_EffectPtr, NULL, NULL);
	if(FAILED(hr))
	{
		MessageBox(NULL, _T("Kan effect niet aanmaken"), NULL, MB_OK);
		return;
	}
	m_TechniekPtr = m_EffectPtr->GetTechniqueByName("Render");

	D3D10_INPUT_ELEMENT_DESC layout[] =
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
    };
    UINT numElements = sizeof( layout ) / sizeof( layout[0] );

    // Create the input layout
    D3D10_PASS_DESC PassDesc;
    m_TechniekPtr->GetPassByIndex( 0 )->GetDesc( &PassDesc );
	hr = DXDevice::GetSingleton()->GetDevice()->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature,
                                          PassDesc.IAInputSignatureSize, &m_VertexLayoutPtr );
    if( FAILED( hr ) )
	{
		MessageBox(NULL, _T("Probleem met de pass"), NULL, MB_OK);
        return;
	}

	SimpleVertex vertices[] = 
	{
		D3DXVECTOR3((m_Pos.x + 0.0f) * m_Scale, (m_Pos.y + 0.5f) * m_Scale, m_Pos.z + 0.5f),
		D3DXVECTOR3((m_Pos.x + 0.5f) * m_Scale, (m_Pos.y + -0.5f) * m_Scale, m_Pos.z + 0.5f),
		D3DXVECTOR3((m_Pos.x + -0.5f) * m_Scale, (m_Pos.y + -0.5f) * m_Scale, m_Pos.z + 0.5f),
	};

	D3D10_BUFFER_DESC bdesc;
	bdesc.Usage = D3D10_USAGE_DEFAULT;
	bdesc.ByteWidth = sizeof(SimpleVertex) * 3;
	bdesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	bdesc.CPUAccessFlags = 0;
	bdesc.MiscFlags = 0;
	D3D10_SUBRESOURCE_DATA subdesc;
	subdesc.pSysMem = vertices;
	hr = DXDevice::GetSingleton()->GetDevice()->CreateBuffer(&bdesc, &subdesc, &m_VertexBufferPtr);
	if(FAILED(hr))
	{
		MessageBox(NULL, _T("ERROR Creating vertex buffer"), NULL, MB_OK);
		return;
	}
}